//
//  EFMenuCollectionView.m
//  MyCollectionView
//
//  Created by xinqing on 15/9/14.
//  Copyright (c) 2015年 pansoft. All rights reserved.
//

#import "EFMenuCollectionView.h"

#define LAYOUT_HEIGHT 90
#define LAYOUT_WIDTH 50
#define SCROLL_DIRECTION UICollectionViewScrollDirectionVertical

@implementation EFMenuCollectionView{
    int i;
    UICollectionViewFlowLayout *_flowLayout;
}

-(instancetype)initWithFrame:(CGRect)frame datas:(NSMutableArray *) stubObjects{
    i = 0;
    self.layoutHeight = LAYOUT_HEIGHT;
    self.layoutWidth = LAYOUT_WIDTH;
    self.scrollDirection = SCROLL_DIRECTION;
    
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self = [super initWithFrame:frame collectionViewLayout:_flowLayout];
    if (self) {
        self.columnCount = 3;
        self.stubObjects = stubObjects;
        [self setBackgroundColor:[UIColor clearColor]];
        
        _cellImageWidth = 50;
        _cellImageHeight = 50;
        _cellTitleHeight = 20;
        _cellTitleFontSize = 16;
        _cellTitleTextColor = [UIColor blackColor];
    }
    return self;
}

#pragma -mark 2 创建组件
-(void)creationComplete{
    if (_flowLayout) {
        _flowLayout.itemSize=CGSizeMake(_layoutWidth,_layoutHeight);
        _flowLayout.minimumInteritemSpacing = 0;
        [_flowLayout setScrollDirection:_scrollDirection];
    }
    self.dataSource = self;
    self.delegate = self;
    [self registerClass:[EFMenuCollectionCell class] forCellWithReuseIdentifier:@"CollectionViewIdentifier"];
}

#pragma -mark -菜单相关
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EFMenuCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewIdentifier" forIndexPath:indexPath];
    
    if(cell.imageWidth == 0 && cell.imageHeight == 0 && cell.titleHeight == 0){
        cell.imageWidth = _cellImageWidth;
        cell.imageHeight = _cellImageHeight;
        cell.titleHeight = _cellTitleHeight;
        cell.titleFontSize = _cellTitleFontSize;
        cell.titleColor = _cellTitleTextColor;
        [cell creationComplete];
//        NSLog(@"cell属性赋值 %d",i++);
    }
    NSInteger index = (indexPath.section * _columnCount + indexPath.row);
    StubObject *currentStubObject = [_stubObjects objectAtIndex:index];
    NSString *imageUrl = [currentStubObject getStringForKey:@"menuIcon" withDefaultValue:@""];
    cell.imagePath = imageUrl;
    cell.title = [currentStubObject getCaption];
//    cell.image = [UIImage imageNamed:@"test_Image"];
//    cell.title = [currentStubObject getStringForKey:@"title" withDefaultValue:@"title"];
    
    int count = [[currentStubObject getStringForKey:KEY_INDICATE_COUNT withDefaultValue:@"0"] intValue];
    cell.indicateCount = count;//99;//count;

    return cell;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSInteger totalSection ;
    int number = _stubObjects.count % _columnCount;
    if (number == 0) {
        totalSection = _stubObjects.count / _columnCount;
    }else{
        totalSection = _stubObjects.count / _columnCount + 1;
    }
    
    if(section == totalSection - 1){
        int count = _stubObjects.count % _columnCount;
        count = count == 0 ? _columnCount : count;
        return count;
    }
    return _columnCount;
}

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSInteger section;
    NSInteger number = _stubObjects.count % _columnCount;
    if (number == 0) {
        section = _stubObjects.count / _columnCount;
    }else{
        section = _stubObjects.count / _columnCount + 1;
    }
    return section;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //调用delegate
    if (_menuCollectionViewDelegate != nil && [_menuCollectionViewDelegate respondsToSelector:@selector(menuCollectionView:didSelectItemAtIndexPath:)]) {
        [_menuCollectionViewDelegate menuCollectionView:self didSelectItemAtIndexPath:indexPath];
    }
}

#pragma -mark 定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1, 1, 0, 0);
}

#pragma -mark 清除右上角所有的指示
-(void)clearAllIndicate{
    for (StubObject *stub in _stubObjects) {
        [stub setString:@"0" forAKey:KEY_INDICATE_COUNT];
    }
    [self reloadData];
}

-(void)clearIndicateAtIndex:(NSInteger)index{
    StubObject *stub = [_stubObjects objectAtIndex:index];
    if (stub != nil) {
        [stub setString:@"0" forAKey:KEY_INDICATE_COUNT];
        [self reloadData];
    }
}

@end
